package ink.tiptop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ink.tiptop.convert.UserBaseInfoConvert;
import ink.tiptop.mapper.UserBaseInfoMapper;
import ink.tiptop.pojo.CommonResult;
import ink.tiptop.pojo.dto.userBaseInfo.AddUserBaseInfoDto;
import ink.tiptop.pojo.dto.userBaseInfo.DeleteUserBaseInfoDto;
import ink.tiptop.pojo.dto.userBaseInfo.QueryUserBaseInfoDto;
import ink.tiptop.pojo.dto.userBaseInfo.UpdateUserBaseInfoDto;
import ink.tiptop.pojo.po.UserBaseInfoPO;
import ink.tiptop.pojo.vo.UserBaseInfoVO;
import ink.tiptop.service.UserBaseInfoService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static ink.tiptop.learn.pojo.CommonResult.success;

/**
 * 测试用户信息表(UserBaseInfo)表服务实现类
 *
 * @author zjf
 * @since 2020-08-23 14:47:13
 */
@Service("userBaseInfoService")
public class UserBaseInfoServiceImpl implements UserBaseInfoService {
    @Resource
    private UserBaseInfoMapper userBaseInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CommonResult queryById(Integer id) {
        return success(this.userBaseInfoMapper.selectById(id));
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public CommonResult deleteById(Integer id) {
        return success(this.userBaseInfoMapper.deleteById(id) > 0);
    }

    @Override
    public CommonResult list(QueryUserBaseInfoDto request) {
        // 转换查询实体对象
        UserBaseInfoPO convert = UserBaseInfoConvert.INSTANCE.convert(request);
        // 查询
        List<UserBaseInfoPO> UserBaseInfoPOS = userBaseInfoMapper.selectList(new QueryWrapper<>(convert));
        // 转换vo
        List<UserBaseInfoVO> UserBaseInfoVOS = UserBaseInfoConvert.INSTANCE.convertList(UserBaseInfoPOS);
        return success(UserBaseInfoVOS);
    }

    public CommonResult update(UpdateUserBaseInfoDto request) {
        Integer count = userBaseInfoMapper.selectCount(new QueryWrapper<>(
                new UserBaseInfoPO().setId(request.getId())));

        if (count == null || count < 1) {
            return CommonResult.FAILED;
        }
        UserBaseInfoPO convert = UserBaseInfoConvert.INSTANCE.convert(request);

        int rows = 0;
        try {
            rows = userBaseInfoMapper.updateById(convert);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return CommonResult.FAILED;
            }
        }
        if (rows > 0) {
            return CommonResult.SUCCESS;
        }
        return CommonResult.FAILED;
    }


    public CommonResult delete(DeleteUserBaseInfoDto request) {
        int rows = userBaseInfoMapper.deleteBatchIds(request.getId());
        if (rows > 0) {
            return CommonResult.SUCCESS;
        }
        return CommonResult.FAILED;
    }

    public CommonResult add(AddUserBaseInfoDto request) {
        UserBaseInfoPO convert = UserBaseInfoConvert.INSTANCE.convert(request);
        int rows = userBaseInfoMapper.insert(convert);
        if (rows > 0) {
            return CommonResult.SUCCESS;
        }
        return CommonResult.FAILED;
    }
}